package org.apache.service.impl;

import java.util.List;

import org.apache.dao.SettledContractDAO;
import org.apache.dao.UserAccountDAO;
import org.apache.model.Contract;
import org.apache.model.Event;
import org.apache.model.SettledContract;
import org.apache.model.UserAccount;
import org.apache.service.AIService;
import org.apache.service.UserAccountService;
import org.apache.util.CalcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AIServiceImpl implements AIService {

    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private UserAccountDAO userAccountDAO;

    @Autowired
    private SettledContractDAO settledContractDAO;

    @Override
    @Transactional
    // production - backup
    // bet ngược kèo bão và cách ít nhất là 6 trái tỉ lệ từ 1-2%
    public Contract getBestContract(List<Contract> qualifyContracts) {

        for (Contract c : qualifyContracts) {
            if (c.getContractType().indexOf("Under") > -1) {
                Event e = c.getEvent();
                int underTotalGoal = CalcUtil.getUnderTotalGoal(c.getContractType());
                if (underTotalGoal >= (e.getTotalGoal() + 5)) {
                    System.out.println(" Contract qualify :" + c.getEvent().getName());
                    return c;
                }
            }
        }
        return null;
    }

    // @Override
    // @Transactional
    // // development
    // // bet ngược kèo bão và cách ít nhất là 6 trái tỉ lệ từ 1-2%
    // public Contract getBestContract(List<Contract> qualifyContracts) {
    //
    // for (Contract c : qualifyContracts) {
    // if (c.getContractType().indexOf("Under") > -1) {
    // Event e = c.getEvent();
    // // int underTotalGoal = CalcUtil.getUnderTotalGoal(c.getContractType());
    // // if (underTotalGoal >= (e.getTotalGoal() + 5)) {
    // System.out.println(" Contract qualify :" + c.getEvent().getName());
    // return c;
    // // }
    // }
    // }
    // return null;
    // }

    @Override
    public void virtualPlaceBet(Contract bestContract) {

        try {
            System.out.println("::::::::::::::::::::::::::::::::::::::::::::");
            System.out.println("::::::::::::::::::::::::::::::::::::::::::::");
            System.out.println("@@ Virtual place bet for best event : " + bestContract.getEvent().getName());
            System.out.println("@@ best contract : " + bestContract.getContractType());
            System.out.println("@@ best rate : " + bestContract.getRate());
            System.out.println("@@ current play time : " + bestContract.getEvent().getPlayTime());
            System.out.println("@@ current goal : " + bestContract.getEvent().getTotalGoal());
            System.out.println("::::::::::::::::::::::::::::::::::::::::::::");
            System.out.println("::::::::::::::::::::::::::::::::::::::::::::");

            Float balance = userAccountService.getBalance();
            SettledContract settledContract = new SettledContract();
            settledContract.setContract(bestContract);
            settledContract.setBetAmount(balance);
            settledContract.setWonAmount(balance * bestContract.getRate());
            settledContract.setResultAmount(0F);
            settledContract.setRunning(true);
            settledContract.setUnderGoal(CalcUtil.getUnderTotalGoal(bestContract.getContractType()));
            settledContractDAO.insert(settledContract);
        } catch (Exception e) {
            System.out.println(" virtualPlaceBet ");
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public Boolean calcResult(SettledContract running) {

        if (running.getCurrentGoal() < running.getUnderGoal()) {
            // won
            running.setWon(true);
            // update balance to bet next match
            UserAccount user = userAccountService.getUser();
            user.setBalance(running.getWonAmount());
            userAccountDAO.update(user);
        } else {
            // lost
            running.setWon(false);
        }
        settledContractDAO.update(running);

        return running.getWon();
    }
}
